package cn.iocoder.yudao.module.eval;

import cn.iocoder.yudao.module.eval.controller.admin.major.vo.MajorImportExcelVO;
import cn.idev.excel.EasyExcel;
import cn.idev.excel.context.AnalysisContext;
import cn.idev.excel.event.AnalysisEventListener;

import java.io.File;
import java.util.Map;

public class TestExcelHeadersDetailed {
    
    public static void main(String[] args) {
        // 注意：这个测试类仅用于本地测试，不会在实际应用中运行
        // 请将实际的Excel文件路径替换下面的路径
        String filePath = "D:\\myProject\\ESCC\\yudao-cloud-master3\\yudao-cloud-master\\企业职业技能等级认定培训评价工作辅助平台\\专业管理导入模板.xls"; // 替换为实际的Excel文件路径
        
        try {
            System.out.println("开始分析Excel文件: " + filePath);
            System.out.println("文件是否存在: " + new File(filePath).exists());
            
            // 检查文件扩展名
            String fileExtension = getFileExtension(filePath);
            System.out.println("文件扩展名: " + fileExtension);
            
            // 创建一个监听器来获取表头信息
            DetailedHeaderAnalysisListener listener = new DetailedHeaderAnalysisListener();
            
            // 读取Excel文件，让EasyExcel自动检测格式
            EasyExcel.read(filePath, MajorImportExcelVO.class, listener)
                    .sheet()
                    .doRead();
            
            // 打印分析结果
            System.out.println("\n=== 分析结果 ===");
            listener.printAnalysisResults();
            
        } catch (Exception e) {
            System.err.println("读取Excel文件时发生错误:");
            e.printStackTrace();
        }
    }
    
    private static String getFileExtension(String fileName) {
        if (fileName == null || fileName.lastIndexOf(".") == -1) {
            return "";
        }
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }
    
    static class DetailedHeaderAnalysisListener extends AnalysisEventListener<MajorImportExcelVO> {
        private Map<Integer, String> headMap;
        private boolean headersAnalyzed = false;
        
        @Override
        public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
            System.out.println("\n=== 表头Map信息 ===");
            this.headMap = headMap;
            System.out.println("表头Map: " + headMap);
            
            // 打印每个表头的详细信息
            for (Map.Entry<Integer, String> entry : headMap.entrySet()) {
                String header = entry.getValue();
                System.out.println("列 " + entry.getKey() + ":");
                System.out.println("  原始值: '" + header + "'");
                System.out.println("  长度: " + (header != null ? header.length() : 0));
                if (header != null) {
                    System.out.println("  字符数组: " + java.util.Arrays.toString(header.toCharArray()));
                    System.out.println("  Unicode编码: ");
                    for (int i = 0; i < header.length(); i++) {
                        System.out.println("    [" + i + "] '" + header.charAt(i) + "' (U+" + String.format("%04X", (int)header.charAt(i)) + ")");
                    }
                }
            }
            
            headersAnalyzed = true;
            super.invokeHeadMap(headMap, context);
        }
        
        @Override
        public void invoke(MajorImportExcelVO data, AnalysisContext context) {
            // 不处理数据，只分析表头
            if (!headersAnalyzed) {
                System.out.println("数据行: " + data);
            }
        }
        
        @Override
        public void doAfterAllAnalysed(AnalysisContext context) {
            System.out.println("\n所有分析完成");
        }
        
        public void printAnalysisResults() {
            if (!headersAnalyzed) {
                System.out.println("未分析到表头信息");
                return;
            }
            
            System.out.println("\n=== 期望的表头 ===");
            String[] expectedHeaders = {
                "大类编码",
                "专业编码", 
                "等级",
                "专业名称",
                "培养目标",
                "学习年限",
                "职业能力",
                "对应或相关职业（工种）",
                "职业资格(职业技能等级)",
                "教学内容与要求",
                "对应上一级专业编码",
                "对应下一级专业编码"
            };
            
            for (int i = 0; i < expectedHeaders.length; i++) {
                String expected = expectedHeaders[i];
                String actual = headMap.get(i);
                System.out.println("列 " + i + ":");
                System.out.println("  期望: '" + expected + "'");
                System.out.println("  实际: '" + actual + "'");
                System.out.println("  匹配: " + (expected.equals(actual) ? "是" : "否"));
                if (actual != null && !expected.equals(actual)) {
                    System.out.println("  差异分析:");
                    compareStrings(expected, actual);
                }
            }
        }
        
        private void compareStrings(String expected, String actual) {
            // 比较长度
            if (expected.length() != actual.length()) {
                System.out.println("    长度不同: 期望=" + expected.length() + ", 实际=" + actual.length());
            }
            
            // 比较每个字符
            int minLength = Math.min(expected.length(), actual.length());
            for (int i = 0; i < minLength; i++) {
                char expChar = expected.charAt(i);
                char actChar = actual.charAt(i);
                if (expChar != actChar) {
                    System.out.println("    字符不同 [" + i + "]: 期望='" + expChar + "' (U+" + String.format("%04X", (int)expChar) + 
                                     "), 实际='" + actChar + "' (U+" + String.format("%04X", (int)actChar) + ")");
                }
            }
            
            // 检查是否有额外字符
            if (expected.length() > actual.length()) {
                System.out.println("    期望字符串更长，多出字符: '" + expected.substring(actual.length()) + "'");
            } else if (actual.length() > expected.length()) {
                System.out.println("    实际字符串更长，多出字符: '" + actual.substring(expected.length()) + "'");
            }
        }
    }
}